package com.whp.openapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whp.openapi.common.ResponseResult;
import com.whp.openapi.common.ResultTable;
import com.whp.openapi.dao.CustomerMapper;
import com.whp.openapi.entity.Customer;
import com.whp.openapi.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author whp
 * @date 2021/11/25
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;
    
    @Override
    public ResultTable table(Integer page, 
                             Integer limit, 
                             String name, 
                             Integer state) {
        PageHelper.startPage(page, limit);
        List<Customer> customers = customerMapper.table(name, state);
        PageInfo pi = new PageInfo(customers);
        return new ResultTable(0, "msg", (int) pi.getTotal(), pi.getList());
    }

    @Override
    public ResponseResult delCustomer(Integer[] ids) {
        int i = customerMapper.delCustomer(ids);
        if (i > 0) {
            return ResponseResult.getSuccess();
        }
        return ResponseResult.getFail("删除失败");
    }

    @Override
    public ResponseResult addCustomer(Customer customer) {
        if (customer != null) {
            int i = customerMapper.insertSelective(customer);
            if (i > 0) {
                return ResponseResult.getSuccess();
            }
        }
        return ResponseResult.getFail("请提供信息");
    }

    @Override
    public ResponseResult updateCustomer(Customer customer) {
        if (customer != null) {
            int i = customerMapper.updateByPrimaryKeySelective(customer);
            if (i > 0) {
                return ResponseResult.getSuccess();
            }
        }
        return ResponseResult.getFail("请提供信息");
    }

    @Override
    public List<Customer> getAllCustomer() {
        return customerMapper.getAllCustomer();
    }
}
